/**
 * 版权所有(C)，中国银联股份有限公司，2002-2013，所有权利保留。
 * 
 * 项目名：	simple-web
 * 文件名：	UserInfoAction.java
 * 模块说明：	
 * 修改历史：
 * 2013-11-17 - Administrator - 创建。
 */
package com.aeky.simple.action.userInfo;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.aeky.simple.action.BaseAction;
import com.aeky.simple.cache.DataCache;
import com.aeky.simple.entity.RoleInfo;
import com.aeky.simple.entity.UserInfo;
import com.aeky.simple.enums.EnableStatusEnum;
import com.aeky.simple.service.UserInfoService;
import com.aeky.simple.util.AccessUtils;
import com.aeky.simple.util.DateUtil;
import com.aeky.simple.vo.page.Pager;

/**
 * 用户信息Action
 * 
 * @author wanghui
 * 
 */
public class UserInfoAction extends BaseAction {

    private static final long serialVersionUID = -8526352466493687803L;

    /** 用户信息 */
    private UserInfo userInfo;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 初始化新增用户信息
     * 
     * @return
     * @throws Exception
     */
    public String initAddUserInfo() throws Exception {
        return "initAdd";
    }

    /**
     * 初始化用户信息编辑
     * 
     * @return
     * @throws Exception
     */
    public String initManageUserInfo() throws Exception {
        return "initManage";
    }

    /**
     * 新增用户信息
     * 
     * @return
     * @throws Exception
     */
    public String addUserInfo() throws Exception {
        userInfo.setRecordCrtOperLoginName(this.getLoginUserName());
        userInfo.setRecordUpdOperLoginName(this.getLoginUserName());
        userInfo.setLastLoginIp(AccessUtils.getClientIp(request));
        try {
            userInfoService.addUserInfo(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ajaxResult = buildJSONResponse("success", "用户添加成功！");
        return AJAX_RESULT;
    }

    /**
     * 初始化编辑用户信息
     * 
     * @return
     * @throws Exception
     */
    public String initEditUserInfo() throws Exception {
        final String userId = request.getParameter("userId");
        final String version = request.getParameter("version");
        UserInfo ui = null;
        try {
            ui = userInfoService.queryUserInfoByIdAndVersion(Long.valueOf(userId),
                            Integer.valueOf(version));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (ui == null) {
            ajaxResult = buildJSONResponse("fail", "数据版本有变，将刷新页面！");
        } else {
            ajaxResult = buildJSONResponse("success",
                            this.getJsonFromObject(converterObjectToMap(ui)));
        }
        return AJAX_RESULT;
    }

    /**
     * 编辑用户信息提交
     * 
     * @return
     * @throws Exception
     */
    public String editUserInfo() throws Exception {
        userInfo.setRecordUpdOperLoginName(getLoginUserName());
        try {
            userInfoService.updateUserInfo(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ajaxResult = buildJSONResponse("success", "操作成功");
        return AJAX_RESULT;
    }

    /**
     * 删除用户信息
     * 
     * @return
     * @throws Exception
     */
    public String deleteUserInfo() throws Exception {
        final String userId = request.getParameter("userId");
        final String version = request.getParameter("version");
        try {
            userInfoService.deleteUserInfo(Long.valueOf(userId), Integer.valueOf(version));
        } catch (Exception e) {
            e.printStackTrace();
        }
        ajaxResult = buildJSONResponse("success", "操作成功");
        return AJAX_RESULT;
    }

    /**
     * 分页查询用户信息列表
     * 
     * @return
     * @throws Exception
     */
    public String listUserInfo() throws Exception {
        final String loginName = request.getParameter("loginName");
        final String realName = request.getParameter("realName");
        Pager<UserInfo> pager = new Pager<UserInfo>(request.getParameter("page"),
                        request.getParameter("rows"));
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("loginName", loginName);
        dataMap.put("realName", realName);
        try {
            pager = userInfoService.queryUserInfoList(pager, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ajaxResult = this.getJsonFromObject(changeToMapPager(pager));
        return AJAX_RESULT;
    }

    public <T> void addOtherMapValue(T t, Map<String, Object> map) {
        UserInfo ui = (UserInfo) t;
        String[] roleIds = ui.getRoleId().split(",");
        Map<Long, RoleInfo> riMap = DataCache.getInstance().getRoleInfoCache();
        String str = "";
        for (String s : roleIds) {
            final String roleName = riMap.get(Long.valueOf(s)).getRoleName();
            if ("".equals(str)) {
                str = roleName;
            } else {
                str += "," + roleName;
            }
        }
        map.put("roleName", str);
        map.put("userStatusName", EnableStatusEnum.getEnumNameByValue(ui.getUserStatus()));
        map.put("lastLoginTime", DateUtil.formatDateDefaultDisplay(ui.getLastLoginTime()));
        map.put("recordCrtTime", DateUtil.formatDateDefaultDisplay(ui.getRecordCrtTime()));
        map.put("recordUpdTime", DateUtil.formatDateDefaultDisplay(ui.getRecordUpdTime()));
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

}
